package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.WebConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysCategory;
import com.ruoyi.system.domain.SysFriends;
import com.ruoyi.system.domain.SysLooper;
import com.ruoyi.system.domain.SysSettings;
import com.ruoyi.system.mapper.SysCategoryMapper;
import com.ruoyi.system.mapper.SysFriendsMapper;
import com.ruoyi.system.mapper.SysLooperMapper;
import com.ruoyi.system.mapper.SysSettingsMapper;
import com.ruoyi.system.service.WebInfoPortalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * webinfo
 *
 * @PackageName com.ruoyi.system.service.impl
 * @ClassName WebInfoPortalServiceImpl
 * @Auther jgg
 * @Date 2022-7-21 14:04
 */
@Service
public class WebInfoPortalServiceImpl implements WebInfoPortalService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysCategoryMapper sysCategoryMapper;
    @Autowired
    private SysLooperMapper looperMapper;

    @Autowired
    private SysFriendsMapper friendsMapper;

    @Autowired
    private SysSettingsMapper settingsMapper;

    /**
     * 获取全部分类
     *
     * @return
     */
    @Override
    public AjaxResult getAllCategory() {
        //从redis中获取
        List<SysCategory> cacheList = redisCache.getCacheList(CacheConstants.SYS_CATEGORY_KEY);
        if (cacheList.isEmpty()) {
            List<SysCategory> sysCategoryList = sysCategoryMapper.selectList(null);
            if (!sysCategoryList.isEmpty()) {
                redisCache.setCacheList(CacheConstants.SYS_CATEGORY_KEY, sysCategoryList);
            }
            return AjaxResult.success(sysCategoryList);
        }
        return AjaxResult.success(cacheList);
    }


    /**
     * looper
     *
     * @return
     */
    @Override
    public AjaxResult getAllLooper() {
        //从redis中获取loop图
        List<Object> cacheList = redisCache.getCacheList(CacheConstants.SYS_LOOPER_KEY);
        if (cacheList.isEmpty()) {
            //数据库中获取
            List<SysLooper> looperList = looperMapper.selectList(null);
            if (!looperList.isEmpty()) {
                redisCache.setCacheList(CacheConstants.SYS_LOOPER_KEY, looperList);
            }
            return AjaxResult.success(looperList);
        }
        return AjaxResult.success(cacheList);
    }


    /**
     * friendsLink
     *
     * @return
     */
    @Override
    public AjaxResult getAllFrrendLinks() {
        //从redis中获取friendlinks
        List<SysFriends> cacheList = redisCache.getCacheList(CacheConstants.SYS_FRIEND_LINKS_KEY);
        if (cacheList.isEmpty()) {
            //从数据库中获取
            List<SysFriends> friendsList = friendsMapper.selectList(null);
            if (!friendsList.isEmpty()) {
                redisCache.setCacheList(CacheConstants.SYS_FRIEND_LINKS_KEY, friendsList);
                return AjaxResult.success(friendsList);
            }
        }
        return AjaxResult.success(cacheList);
    }


    /**
     * 获取网站seo信息
     *
     * @return
     */
    @Override
    public AjaxResult getWebSeo() {
        SysSettings description = settingsMapper.selectOne(new LambdaQueryWrapper<SysSettings>().eq(SysSettings::getKey, WebConstants.settings.WEB_SIZE_DESCRIPTION));
        SysSettings keyword = settingsMapper.selectOne(new LambdaQueryWrapper<SysSettings>().eq(SysSettings::getKey, WebConstants.settings.WEB_SIZE_KEYWORDS));
        Map<String, String> result = new HashMap<>();
        if (description != null && keyword != null) {

            result.put(description.getKey(), description.getValue());
            result.put(keyword.getKey(), keyword.getValue());
        }
        return AjaxResult.success(result);
    }


    /**
     * title
     *
     * @return
     */
    @Override
    public AjaxResult getWebTitle() {
        SysSettings title = settingsMapper.selectOne(new LambdaQueryWrapper<SysSettings>().eq(SysSettings::getKey, WebConstants.settings.WEB_SIZE_TITLE));
        return AjaxResult.success(title);
    }
}
